package validator;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import command.EventForm;

@Component
public class EventFormValidator implements Validator {

	@Override
	public boolean supports(Class<?> clazz) {
		return EventForm.class.equals(clazz);
	}
	
	@Override
	public void validate(Object target, Errors errors) {
		EventForm form = (EventForm) target;
		if (form.getName() == null || form.getName().isEmpty()) {
			errors.rejectValue("name", "empty");
		} else if (form.getName().length() > 100) {
			errors.rejectValue("name", "tooLong");
		}
		if (form.getDescription() != null && form.getDescription().length() > 10000) {
			errors.rejectValue("description", "tooLong");
		}
		if ((form.getToUsers() == null || form.getToUsers().size() == 0) &&
				(form.getToProfiles() == null || form.getToProfiles().size() == 0)) {
			errors.rejectValue("toUsers", "empty");
		}
	}
}